"""
更新数据
"""
import MySQLdb

no = int(input('编号：'))
location = input('所在地：')

# 1. 创建连接对象
conn = MySQLdb.connect(host='47.104.31.138', port=3306,
                       user='root', password='Luohao.618',
                       database='hrs', charset='utf8')
try:
    # 2. 获取游标对象
    with conn.cursor() as cursor:
        # 3. 通过游标对象向数据库发出SQL语句获取执行结果
        affected_rows = cursor.execute(
            'update tb_dept set dloc=%s where dno=%s',
            (location, no)
        )
        if affected_rows == 1:
            print('更新部门成功')
    # 4. 提交事务
    conn.commit()
except MySQLdb.MySQLError as err:
    print(err)
    # 4. 回滚事务
    conn.rollback()
finally:
    # 5. 关闭数据库连接（释放资源）
    conn.close()
